<?php
defined('ROOTPATH') OR exit('Forbidden');

// 载入数据库配置文件
require_once ROOTPATH.'config/cfg_database.php';

class db {

    protected static $table = null;
    protected static $sql = null;
    protected static $result = null;

    public static function query($query_sql) {
        $db = cfg_database::$cfg_db;

        $link = mysqli_connect($db['url'], $db['username'], $db['password'], $db['schema']) or die("database error!" . mysqli_error($link));
        mysqli_query($link,"SET NAMES UTF8");

        $query = "$query_sql" or die("sql error!" . mysqli_error($link));
        self::$result = mysqli_query($link, $query);
    }

    public static function getone() {
        return mysqli_fetch_assoc(self::$result);
    }

    public static function getall() {
        $k = 0;
        while($row = mysqli_fetch_array(self::$result, MYSQLI_ASSOC)) {
        	$array_result[$k] = $row;
            $k++;
        }
        return $array_result;
    }

    public static function table($tname) {
        self::$table = $tname;
    }

    public static function where($fields) {
        $table_name = self::$table;
        $where = "";
        if (!empty($fields)) {
            foreach ($fields as $k => $v) {
                $field = explode(' ', $k)[0];
                $symbol = explode(' ', $k)[1] ? explode(' ', $k)[1] : "=";
                $where .= " AND `{$field}` $symbol '{$v}'";
            }
        }
        self::$sql = "SELECT * FROM `{$table_name}` WHERE 1" . $where;
        self::query(self::$sql);
    }

    public static function insert($fields) {
        $table_name = self::$table;

        $keys = "";
        $values = "";
        foreach ($fields as $k => $v) {
            $keys .= "`".$k."`,";
            $values .= "\"".$v."\",";
        }
        $keys = substr($keys, 0, strlen($keys)-1);
        $values = substr($values, 0, strlen($values)-1);

        self::$sql = "INSERT INTO `{$table_name}` ({$keys}) VALUES ({$values})";
        self::query(self::$sql);
    }

    public static function update($fields, $where_fields) {
        $table_name = self::$table;

        $strings = "";
        foreach ($fields as $k => $v) {
            $strings .= "`".$k."`=\"".$v."\",";
        }
        $strings = substr($strings, 0, strlen($strings)-1);

        $where = "";
        if (!empty($where_fields)) {
            foreach ($where_fields as $k => $v) {
                $where_field = explode(' ', $k)[0];
                $symbol = explode(' ', $k)[1] ? explode(' ', $k)[1] : "=";
                $where .= " AND `{$where_field}` $symbol '{$v}'";
            }
        }

        self::$sql = "UPDATE `{$table_name}` SET {$strings} WHERE 1" . $where;
        self::query(self::$sql);
    }

    public static function delete($fields) {
        $table_name = self::$table;
        $where = "";
        if (!empty($fields)) {
            foreach ($fields as $k => $v) {
                $field = explode(' ', $k)[0];
                $symbol = explode(' ', $k)[1] ? explode(' ', $k)[1] : "=";
                $where .= " AND `{$field}` $symbol '{$v}'";
            }
        }
        self::$sql = "DELETE FROM `{$table_name}` WHERE 1" . $where;
        self::query(self::$sql);
    }
}
